// app.js
App({
  globalData: {
    url: 'https://www.h5fd.xyz:2021',
    bgurl:'https://www.h5fd.xyz:2021/app/bg/bg.jpg',
    userInfo: {},
    hasUserInfo: false,
    taskList:[],
    w:0,
    h:0,
    h2:0
  },
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    if(!wx.getStorageSync('login_key')){
      // 获取login_key
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          if (res.code) {
            //发起网络请求
            wx.request({
              url: `https://www.h5fd.xyz:2021/mp-login/${res.code}`,
              success: function (res) {
                wx.setStorageSync("login_key", res.data)
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })
    }else{
      wx.request({
        url: 'https://www.h5fd.xyz:2021/user/getUser',
        header:{'Content-Type':'application/x-www-from-urlencoded','authorization':`${wx.getStorageSync('login_key')}`},
        success:res=>{
          if(res.statusCode==200){
            if(res.data){
              this.globalData.userInfo = res.data;
              this.globalData.hasUserInfo = true
            }
          }else if(res.statusCode==403){
            // 重新获取login_key
            wx.login({
              success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                if (res.code) {
                  console.log(res)
                  //发起网络请求
                  wx.request({
                    url: `https://www.h5fd.xyz:2021/mp-login/${res.code}`,
                    data: {},
                    success: function (res) {
                      wx.setStorageSync("login_key", res.data)
                    }
                  })
                } else {
                  console.log('登录失败！' + res.errMsg)
                }
              }
            })

          }
        }
      })
      wx.request({
        url: 'https://www.h5fd.xyz:2021/user/userBg',
        header:{'Content-Type':'application/x-www-from-urlencoded','authorization':`${wx.getStorageSync('login_key')}`},
        success:res=>{
          console.log(res.data)
          if(res.data!=='no data'){
            this.globalData.bgurl=`https://www.h5fd.xyz:2021/app/bg/${res.data.bgname}`
          }
        }
      })
    }

    //获取系统信息，获取窗口宽高度
    wx.getSystemInfo({
      success: (res) => {
        this.globalData.w = res.windowWidth;
        this.globalData.h = res.windowHeight;
        this.globalData.h2 =res.safeArea.height;
      },
    })
  },
  /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
    },
})